import axios from 'axios';
import * as cheerio from 'cheerio';

export class ReptileHtml {
  static async getHtml(url: string) {
    const res = await axios.get(url);
    const $ = cheerio.load(res.data);
    const { initialState } = JSON.parse($('#js-initialData').text());
    // console.log(initialState.daily.story);
    initialState.daily.story.hint = '作者 / ' + $('.author').text();
    return initialState.daily.story;
  }

  static async getStoryList(url: string) {
    const res = await axios.get(url);
    const $ = cheerio.load(res.data);
    const list = [];
    $('.link-button').each((i, el) => {
      list.push('https://daily.zhihu.com' + $(el).attr('href'));
    });
    return list;
  }
}
